package com.solocredit.finance.base.utils

import android.content.Context
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.solocredit.finance.R
import com.solocredit.finance.base.utils.log.MullaLog

/**
 * 图片加载相关操作
 */
class ImageUtil {
    companion object {
        private var imageUtil: ImageUtil? = null
        @get:Synchronized
        val instance: ImageUtil?
            get() {
                if (imageUtil == null) {
                    imageUtil = ImageUtil()
                }
                return imageUtil
            }
    }

    /**
     * 加载网络图片
     */
    fun loadImageUrl(context: Context, imageView: ImageView, imgUrl: String) {
        loadImageUrl(context, imageView, imgUrl, R.mipmap.icon_holder)
    }

    /**
     * 加载网络图片
     */
    fun loadImageUrl(context: Context, imageView: ImageView, imgUrl: String, placeholder: Int) {
        if (imgUrl.isEmpty()){
            imageView.setImageResource(R.mipmap.icon_holder)
            return
        }
        Glide.with(context).load(imgUrl).placeholder(placeholder).into(imageView)
    }

    /**
     * 加载本地图片
     */
    fun loadImageRes(context: Context, imageView: ImageView, imgRes: Int) {
        try {
            Glide.with(context).load(imgRes).into(imageView)
        } catch (e: Exception){
            MullaLog.e("activity is destroyed !")
        }
    }
}
